/*
 * Copyright (c) 2009-2011, Petr Panteleyev <petr@panteleyev.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.blogclient.photo;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.DefaultCellEditor;
import javax.swing.DropMode;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

import org.panteleyev.photo.Thumbnail;
import org.panteleyev.swing.TableUtil;


/**
 *
 * @author Petr Panteleyev <petr@panteleyev.org>
 */
public class ThumbnailsPublishPanel<T> extends JPanel {
    private AbstractTableModel model;
    private JTable table;
    private ArrayList<Thumbnail<T>> thumbnails = new ArrayList<>();

    public ThumbnailsPublishPanel() {
        setLayout(new BorderLayout());

        table = new JTable();
        JScrollPane scroll = new JScrollPane(table);
        table.setDragEnabled(true);
        table.setDropMode(DropMode.ON_OR_INSERT_ROWS);

        table.setBackground(Color.WHITE);
        table.setSelectionBackground(Color.WHITE);
        table.setSelectionForeground(Color.BLACK);
        table.setGridColor(Color.WHITE);

        table.setModel(model = new AbstractTableModel(){
            @Override
            public boolean isCellEditable(int row, int column) {
                return (column == 1);
            }

            @Override
            public int getColumnCount() {
                return 2;
            }

            @Override
            public Class<?> getColumnClass(int col) {
                return (col == 0)? ImageIcon.class : String.class;
            }

            @Override
            public int getRowCount() {
                return thumbnails.size();
            }

            @Override
            public Object getValueAt(int row, int column) {
                if (column == 0) {
                    return thumbnails.get(row).getImage();
                } else {
                    return thumbnails.get(row).getTitle();
                }
            }

            @Override
            public void setValueAt(Object aValue, int row, int column) {
                thumbnails.get(row).setTitle((String)aValue);
            }
        });

        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setCellSelectionEnabled(false);
        table.setRowSelectionAllowed(true);
        table.setTableHeader(null);

        table.setCellEditor(new DefaultCellEditor(new JTextField()));

        scroll.setPreferredSize(new Dimension(0, 0));
        add(scroll);

        TableUtil.adjustTableColumnSizes(table);
        TableUtil.adjustTableRowSizes(table);
        table.setTransferHandler(new PanelFromListTransferHandler(this));
    }

    public Thumbnail<T> getThumbnail(int index) {
        return thumbnails.get(index);
    }

    void swap(int i1, int i2) {
        Thumbnail<T> src = thumbnails.get(i1);
        Thumbnail<T> dst = thumbnails.get(i2);
        thumbnails.set(i2, src);
        thumbnails.set(i1, dst);
        update();
    }

    public void update() {
        model.fireTableDataChanged();
        TableUtil.adjustTableColumnSizes(table);
        TableUtil.adjustTableRowSizes(table);
    }

    public void setContent(ArrayList<Thumbnail<T>> thumbnails) {
        this.thumbnails = thumbnails;
        update();
    }
}
